<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>拖拽</title>
    <style>
      .container {
        display: flex;
        flex-direction: column;
        justify-content: center;
        margin-top: 100px;
      }

      .draw-box {
        width: 80%;
        height: 300px;
        border: 2px solid pink;
        position: relative;
      }
      /* .draw-box::before {
        content: "";
        border: 2px solid red;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        width: 96%;
        height: 96%;
      } */
      #draw {
        width: 80px;
        height: 80px;
        background: pink;
        border-radius: 50%;
      }
    </style>
  </head>

  <body>
    <div class="container">
      <div class="draw-box" id="draw-box"></div>
      <div class="draw-box" id="draw-box2"></div>
      <button id="addDraw">增加</button>
    </div>
    <script type="module" src="./js/draw.js"></script>
    <script type="module">
      import { Draw, DrawItem } from "./js/draw.js";
      const drawBox = document.getElementById("draw-box");
      const drawBox2 = document.getElementById("draw-box2");
      const draw1 = new Draw(drawBox, [
        {
          tagName: "div",
          style: {
            width: "70px",
            height: "70px",
            backgroundColor: "pink",
          },
          position: {
            x: 1200,
            y: -10,
          },
        },
      ]);
      const draw2 = new Draw(drawBox2, [
        {
          tagName: "div",
          style: {
            width: "70px",
            height: "70px",
            backgroundColor: "red",
          },
          position: {
            x: 1200,
            y: -10,
          },
        },
      ]);
      document.getElementById("addDraw").onclick = () => {
        // const addDraw = new Draw(drawBox, [
        // {
        //   tagName: "div",
        //   style: {
        //     width: "70px",
        //     height: "70px",
        //     backgroundColor: "black",
        //   },
        //   position: {
        //     x: 0,
        //     y: 0,
        //   },
        // },
        // ]);
        draw1.add({
          tagName: "div",
          style: {
            width: "70px",
            height: "70px",
            backgroundColor: "red",
          },
          position: {
            x: 0,
            y: 0,
          },
        });
      };
    </script>
    <!-- <script>
      const drawBox = document.getElementById("draw-box");
      const draw = document.getElementById("draw");
      const mouse = {
        down: false,
        startX: 0,
        endY: 0,
        moveX: 0,
        moveY: 0,
        boundary: {}, //边界
      };
      function getDrawBoundary() {
        const { top, left, right, bottom, width } =
          drawBox.getBoundingClientRect();
        const drawWidth = draw.offsetWidth;
        const drawHeight = draw.offsetHeight;
        mouse.boundary = {
          right: right - left - drawWidth,
          bottom: bottom - top - drawHeight,
        };
      }
      getDrawBoundary();
      draw.onmousedown = (event) => {
        mouse.startX = event.x - mouse.startX;
        mouse.endY = event.y - mouse.endY;
        mouse.down = true;
      };
      window.onmousemove = (event) => {
        if (!mouse.down) {
          return;
        }
        const { right, bottom, top } = mouse.boundary;
        const moveX = Math.max(Math.min(event.x - mouse.startX, right), 0);
        const moveY = Math.max(Math.min(event.y - mouse.endY, bottom), 0);
        mouse.moveX = moveX;
        mouse.moveY = moveY;
        draw.style.transform = `translate(${moveX}px, ${moveY}px)`;
      };
      window.onmouseup = (event) => {
        event.preventDefault();

        if (!mouse.down) {
          return;
        }
        mouse.down = false;
        mouse.startX = mouse.moveX;
        mouse.endY = mouse.moveY;
      };
    </script> -->
  </body>
</html>
